package code20_jdbc.改写学生管理.dao;

import java.util.List;

import code20_jdbc.改写学生管理.entity.Student;
import code20_jdbc.改写学生管理.mapper.StudentMapper;
import code20_jdbc.改写学生管理.util.JdbcTemplate;
import code20_jdbc.改写学生管理.util.RowMapper;

/*
 * DAO：Data Access Object 数据访问对象
 * 
 * 对学生表的数据访问操作
 */
public class StudentDao {
	JdbcTemplate<Student> template = new JdbcTemplate<Student>();
	RowMapper<Student> rm = new StudentMapper();
	
	public void insert(Student stu){
		String sql = new StringBuffer()
			.append(" insert into t_student ")
			.append(" 	(name,age,height,birthday,cid) ")
			.append(" values ")
			.append(" 	(?,?,?,?,?) ")
			.toString();
		template.update(sql, stu.getName(),stu.getAge(),stu.getHeight(),stu.getBirthday(),stu.getClazz().getCid());
	}
	
	public void deleteById(int id){
		String sql = new StringBuffer()
			.append(" delete from ")
			.append(" 	t_student ")
			.append(" where ")
			.append(" 	id=? ")
			.toString();
		template.update(sql, id);
	}

	public List<Student> selectAll(){
		String sql = new StringBuffer()
			.append(" select ")
			.append(" 	s.id, ")
			.append(" 	s.name, ")
			.append(" 	s.age, ")
			.append(" 	s.height, ")
			.append(" 	s.birthday, ")
			.append(" 	c.cid, ")
			.append(" 	c.cname, ")
			.append(" 	c.room ")
			.append(" from ")
			.append(" 	t_student s,t_class c ")
			.append(" where ")
			.append(" 	s.cid=c.cid ")
			.toString();
		return template.query(sql, rm);
	}
	
	public Student selectById(int id){
		String sql = new StringBuffer()
			.append(" select ")
			.append(" 	s.id, ")
			.append(" 	s.name, ")
			.append(" 	s.age, ")
			.append(" 	s.height, ")
			.append(" 	s.birthday, ")
			.append(" 	c.cid, ")
			.append(" 	c.cname, ")
			.append(" 	c.room ")
			.append(" from ")
			.append(" 	t_student s,t_class c ")
			.append(" where ")
			.append(" 	s.cid=c.cid ")
			.append(" 		and ")
			.append(" 	s.id=? ")
			.toString();
		return template.queryForObject(sql, rm, id);
		
	}
}
